﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ras2Logic;

namespace Ras2Win
{
    /// <summary>
    /// Klasinn PerfomerView - UserControl
    /// Inniheldur eitt listview
    /// </summary>
    public partial class PerformerView : UserControl
    {
        #region Member breytur

        #endregion

        #region Smiðir

        /// <summary>
        /// Færibreytulaus smiður
        /// </summary>
        public PerformerView()
        {
            InitializeComponent();
        }

        #endregion

        #region Get / Set föll

        #endregion

        #region Private föll

        /// <summary>
        /// Fall sem keyrist þegar view er loadað
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PerformerView_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                PerformerController ctrl = new PerformerController();
                foreach (var item in ctrl.GetAllPerformers())
                {
                    m_lstViewPerformers.Items.Add(CreateListViewItem(item));
                }
            }
        }

        /// <summary>
        /// Hjálparfall sem smíðar listview item
        /// </summary>
        /// <param name="performer">Tekur inn Performer object</param>
        /// <returns>Skilar ListViewItem</returns>
        private ListViewItem CreateListViewItem(Performer performer)
        {
            ListViewItem item = new ListViewItem(Convert.ToString(performer.ID));
            item.SubItems.Add(performer.Name);

            item.ImageIndex = 0;
            item.Tag = performer;
            return item;
        }

        #endregion

        #region Public föll

        /// <summary>
        /// Fall sem editar Perfomrerinn sem er valin.
        /// </summary>
        public void OnEditClick()
        {
            if (m_lstViewPerformers.SelectedItems.Count == 1)
            {
                ListViewItem listViewItem = m_lstViewPerformers.SelectedItems[0];

                Performer performer = (Performer)listViewItem.Tag;
                using (EditPerformer dlg = new EditPerformer())
                {
                    dlg.Performer = performer;
                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        // Sækjum gögnin aftur úr dialog
                        performer = dlg.Performer;

                        PerformerController ctrl = new PerformerController();

                        // Látum vinnslulagið uppfæra nemandann. 
                        //TODO: setja try catch
                        ctrl.UpdatePerformer(performer);

                        //Grípa indexinn.
                        int nIndex = listViewItem.Index;
                        // Við uppfærum færsluna með því að fjarlægja þá sem
                        // er fyrir og setja nýja inn:
                        m_lstViewPerformers.Items.Remove(listViewItem);
                        m_lstViewPerformers.Items.Insert(nIndex, CreateListViewItem(performer));
                    }
                }
            }
        }

        /// <summary>
        /// Fall sem setur inn nýjan Performer
        /// </summary>
        public void OnNewClick()
        {
            using (EditPerformer dlg = new EditPerformer())
            {
                dlg.Performer = new Performer();
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    Performer performer = dlg.Performer;
                    PerformerController controller = new PerformerController();

                    // Ef þetta klikkar verður kastað villu:
                    //controller.AddStudent(student);
                    controller.AddPerformer(performer);
                    m_lstViewPerformers.Items.Add(CreateListViewItem(performer));
                }
            }
        }

        #endregion

    }
}
